package lp.xian.materials.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import lp.xian.materials.domain.*;
import lp.xian.materials.mapper.QualityMapper;
import lp.xian.materials.mapper.QualityRawMapper;
import lp.xian.materials.vo.WareVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lp.xian.materials.mapper.WarehousingRequisitionMapper;
import lp.xian.materials.service.IWarehousingRequisitionService;

/**
 * 采购入库单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-10
 */
@Service
public class WarehousingRequisitionServiceImpl implements IWarehousingRequisitionService
{
    @Autowired
    private WarehousingRequisitionMapper warehousingRequisitionMapper;
    @Autowired
    private QualityMapper qualityMapper;
    @Autowired
    private QualityRawMapper qualityRawMapper;


    /**
     * 查询采购入库单
     *
     * @param warehousingRequisitionId 采购入库单主键
     * @return 采购入库单
     */
    @Override
    public WarehousingRequisition selectWarehousingRequisitionByWarehousingRequisitionId(Integer warehousingRequisitionId)
    {
        return warehousingRequisitionMapper.selectWarehousingRequisitionByWarehousingRequisitionId(warehousingRequisitionId);
    }

    /**
     * 查询采购入库单列表
     *
     * @param warehousingRequisition 采购入库单
     * @return 采购入库单
     */
    @Override
    public List<WarehousingRequisition> selectWarehousingRequisitionList(WarehousingRequisition warehousingRequisition)
    {
        return warehousingRequisitionMapper.selectWarehousingRequisitionList(warehousingRequisition);
    }

    /**
     * 新增采购入库单
     *
     * @param warehousingRequisition 采购入库单
     * @return 结果
     */
    @Override
    public int insertWarehousingRequisition(WarehousingRequisition warehousingRequisition)
    {
        warehousingRequisition.setCreateTime(DateUtils.getNowDate());
        return warehousingRequisitionMapper.insertWarehousingRequisition(warehousingRequisition);
    }

    /**
     * 修改采购入库单
     *
     * @param warehousingRequisition 采购入库单
     * @return 结果
     */
    @Override
    public int updateWarehousingRequisition(WarehousingRequisition warehousingRequisition)
    {
        warehousingRequisition.setUpdateTime(DateUtils.getNowDate());
        return warehousingRequisitionMapper.updateWarehousingRequisition(warehousingRequisition);
    }

    /**
     * 批量删除采购入库单
     *
     * @param warehousingRequisitionIds 需要删除的采购入库单主键
     * @return 结果
     */
    @Override
    public int deleteWarehousingRequisitionByWarehousingRequisitionIds(Integer[] warehousingRequisitionIds)
    {
        return warehousingRequisitionMapper.deleteWarehousingRequisitionByWarehousingRequisitionIds(warehousingRequisitionIds);
    }

    /**
     * 删除采购入库单信息
     *
     * @param warehousingRequisitionId 采购入库单主键
     * @return 结果
     */
    @Override
    public int deleteWarehousingRequisitionByWarehousingRequisitionId(Integer warehousingRequisitionId)
    {
        return warehousingRequisitionMapper.deleteWarehousingRequisitionByWarehousingRequisitionId(warehousingRequisitionId);
    }

    @Override
    public int auditWare(WareVo wareVo) {
        List<WarehousingRequisitionTable> ware = wareVo.getOrderList();
        WarehousingRequisition warehousingRequisition = new WarehousingRequisition();
        warehousingRequisition.setWarehousingRequisitionId(wareVo.getWarehousingRequisitionId());
        warehousingRequisition.setAuditStatus(1);
        warehousingRequisition.setAuditTime(new Date());
        warehousingRequisition.setAuditPerson(SecurityUtils.getUsername());
        warehousingRequisitionMapper.updateWarehousingRequisition(warehousingRequisition);
        if (warehousingRequisition.getAuditStatus().intValue() == 1) {
            Quality quality = new Quality();
            quality.setWarehouseDeptId(wareVo.getWarehouseDeptId());
            quality.setWarehousingRequisitionId(wareVo.getWarehousingRequisitionId());
            quality.setWarehousingRequisitionOrder(wareVo.getWarehousingRequisitionOrder());
            quality.setQualityName(wareVo.getWarehousingRequisitionPerson());
            quality.setSupplierId(wareVo.getSupplierId());
            quality.setQualityType(0);
            quality.setQualityStatus(0);
            quality.setCreateBy(SecurityUtils.getUsername());
            quality.setCreateTime(new Date());
            qualityMapper.insertQuality(quality);
            for (int i = 0; i < ware.toArray().length; i++) {
                    QualityRaw qualityRaw = new QualityRaw();
                    qualityRaw.setWarehousingRequisitionTableId(ware.get(i).getWarehousingRequisitionTableId());
                    qualityRaw.setQualityId(quality.getQualityId());
                    qualityRaw.setQualityName(ware.get(i).getWarehousingRequisitionTableName());
                    qualityRaw.setQualitySpeion(ware.get(i).getWarehousingRequisitionTableSpeion());
                    qualityRaw.setQualityNum(ware.get(i).getWarehousingRequisitionTableNum());
                    qualityRaw.setCreateBy(SecurityUtils.getUsername());
                    qualityRaw.setCreateTime(new Date());
                    qualityRawMapper.insertQualityRaw(qualityRaw);

            }

        }
        return 1;
    }
}
